﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKArchMethodLibrary.Utils;
using YJKFamilyLibDataManager;
using YJKPreviewerControl;


namespace DoorAndWindow
{
	public partial class DlgWinDoorInsert : System.Windows.Forms.Form
	{
		public bool AddTag
		{
			get
			{
				return this.chbAddTag.Checked;
			}
		}

		public DbFamily TagDbFamily
		{
			get
			{
				return (DbFamily)this.cbxSymbol.SelectedItem;
			}
		}

		public string FirstTagString
		{
			get
			{
				return this.tbxStart.Text.Trim();
			}
		}

		public string SecondTagString
		{
			get
			{
				return this.tbxTag.Text.Trim();
			}
		}

		public DlgWinDoorInsert(UIApplication uiApp, bool boDoor)
		{
			this.InitializeComponent();
			this.pictureBox1 = new Previewer(1u);
			this.pictureBox1.Paint += this.pictureBox1_Paint;
			this.groupBox1.Controls.Add(this.pictureBox1);
			this.pictureBox1.Location = new System.Drawing.Point(5, 21);
			this.pictureBox1.Size = new Size(this.pbPreview.Width, this.pbPreview.Height);
			this.pictureBox1.TabStop = false;
			//base.Icon = Resource.YArchitechIcon;
			try
			{
				this.m_application = uiApp;
				this.m_document = uiApp.ActiveUIDocument.Document;
				this.m_datum = new DoorWinDatum();
				this.archDB = FamilyLibDatabase.GetSqliteDB(SubjectType.Architecture, this.m_application.Application.VersionNumber);
				this.sysDB = FamilyLibDatabase.GetSqliteDB(SubjectType.System, this.m_application.Application.VersionNumber);
				this.tempInfo = Assist.FileUtil.GetTempDataFile();
				this.cbxPrompt.Clear();
				this.cbxPrompt.AddItem("1");
				this.cbxPrompt.AddItem("2");
				this.cbxPrompt.AddItem("3");
				this.cbxPrompt.AddItem("4");
				this.cbxPrompt.AddItem("5");
				this.cbxPrompt.AddItem("6");
				this.cbxPrompt.AddItem("7");
				this.cbxPrompt.AddItem("8");
				this.cbxPrompt.AddItem("9");
				this.cbxPrompt.AddItem("10");
				this.cbxDis.Clear();
				this.cbxDis.AddItem("0");
				this.cbxDis.AddItem("60");
				this.cbxDis.AddItem("100");
				this.cbxDis.AddItem("120");
				this.cbxDis.AddItem("180");
				this.cbxDis.AddItem("200");
				this.cbxDis.AddItem("240");
				this.cbxDis.AddItem("300");
				this.cbxDis.AddItem("360");
				this.cbxDis.AddItem("400");
				this.cbxDis.AddItem("500");
				this.cbxDis.AddItem("600");
				this.cbxDis.AddItem("700");
				this.cbxDis.AddItem("800");
				this.cbxDis.AddItem("900");
				this.cbxDis.AddItem("1000");
				this.isDoor = boDoor;
				if (boDoor)
				{
					this.Text = "插入门";
					this.labType.Text = "门槛";
					this.initFamilyListByDoc();
					this.initFamilyListByFamilyLib();
					this.initFamilyList();
					this.initCbxDoorHeight();
					this.initDoorInsertType();
					this.initDoorCbxDis();
					this.initDoorCbxPrompt();
					this.cbxHeight.AddItem("0");
					this.cbxHeight.AddItem("50");
					this.cbxHeight.AddItem("60");
					this.cbxHeight.AddItem("100");
					this.cbxHeight.AddItem("150");
					this.cbxHeight.AddItem("200");
					this.cbxHeight.AddItem("300");
					this.cbxHeight.AddItem("600");
				}
				else
				{
					this.Text = "插入窗";
					this.labType.Text = "窗台";
					this.initFamilyListByDoc();
					this.initFamilyListByFamilyLib();
					this.initFamilyList();
					this.initCbxWinHeight();
					this.initWinInsertType();
					this.initWinCbxDis();
					this.initWinCbxPrompt();
					this.cbxHeight.AddItem("0");
					this.cbxHeight.AddItem("300");
					this.cbxHeight.AddItem("400");
					this.cbxHeight.AddItem("500");
					this.cbxHeight.AddItem("600");
					this.cbxHeight.AddItem("700");
					this.cbxHeight.AddItem("800");
					this.cbxHeight.AddItem("900");
					this.cbxHeight.AddItem("1000");
					this.cbxHeight.AddItem("1200");
				}
				this.chbAddTag.Checked = false;
				this.InitType(boDoor);
				this.initTagInfo();
				this.imgW = this.pbPreview.Width;
				this.imgH = this.pbPreview.Height;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private void pictureBox1_Paint(object sender, PaintEventArgs e)
		{
			Graphics graphics = e.Graphics;
            graphics.Clear(System.Drawing.Color.White);
			this.RefreshPreview(graphics);
		}

		private void RefreshPreview(Graphics g)
		{
			try
			{
				if (this.InfoChanged)
				{
					this.InfoChanged = false;
					this.pictureBox1.Curves = this.legend.Curves;
					this.pictureBox1.ZoomExtents();
				}
			}
			catch (Exception)
			{
			}
		}

		private void DlgWinDoorInsert_Load(object sender, EventArgs e)
		{
		}

		private void initFamilyListByDoc()
		{
			string text;
			BuiltInCategory builtInCategory;
			if (this.isDoor)
			{
				text = "门";
				builtInCategory = BuiltInCategory.OST_Doors;
			}
			else
			{
				text = "窗";
				builtInCategory = BuiltInCategory.OST_Windows;
			}
			this.tvwFamilyList.Nodes.Clear();
			TreeNode treeNode = new TreeNode();
			treeNode.Text = text;
			List<Family> list = new List<Family>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_document);
			IList<Element> list2 = filteredElementCollector.OfClass(typeof(FamilySymbol)).OfCategory(builtInCategory).ToElements();
			foreach (Element element in list2)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				if (!list.Contains(familySymbol.Family, new FamilyIdComparer()))
				{
					list.Add(familySymbol.Family);
				}
			}
			foreach (Family family in list)
			{
				TreeNode treeNode2 = new TreeNode();
				treeNode2.Text = family.Name;
				treeNode2.Tag = family;
				List<FamilySymbol> symbols = family.GetSymbols();
				List<FamilySymbol> list3 = (from ff in symbols
				orderby ff.Name
				select ff).ToList<FamilySymbol>();
				foreach (FamilySymbol familySymbol2 in list3)
				{
					TreeNode treeNode3 = new TreeNode();
					treeNode3.Text = familySymbol2.Name;
					treeNode3.Tag = familySymbol2;
					treeNode2.Nodes.Add(treeNode3);
				}
				treeNode.Nodes.Add(treeNode2);
			}
			this.nodesDoc = treeNode;
		}

		private void initFamilyListByFamilyLib()
		{
			try
			{
				string text;
				if (this.isDoor)
				{
					text = "门";
				}
				else
				{
					text = "窗";
				}
				this.tvwFamilyList.Nodes.Clear();
				TreeNode treeNode = new TreeNode();
				treeNode.Text = text;
				DbCategory dbCategory = this.archDB.CategoryTable[text, true];
				List<DbFamily> allFamilies = dbCategory.GetAllFamilies(true, CheckType.All);
				foreach (DbFamily dbFamily in allFamilies)
				{
					TreeNode treeNode2 = new TreeNode();
					treeNode2.Text = dbFamily.Name;
					treeNode2.Tag = dbFamily;
					TreeNode treeNode3 = new TreeNode();
					treeNode3.Text = "Temp";
					treeNode2.Nodes.Add(treeNode3);
					treeNode.Nodes.Add(treeNode2);
				}
				this.nodesFamilyLib = treeNode;
			}
			catch
			{
			}
		}

		private void initFamilyList()
		{
			this.tvwFamilyList.Nodes.Add(this.nodesFamilyLib);
			this.tvwFamilyList.Nodes[0].Expand();
			string noodName;
			if (this.isDoor)
			{
				noodName = "门插入";
			}
			else
			{
				noodName = "窗插入";
			}
			string dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, noodName, "族来源");
			string dataFromTempFile2 = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, noodName, "族名");
			string dataFromTempFile3 = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, noodName, "类型名");
			if (string.IsNullOrEmpty(dataFromTempFile) || string.IsNullOrEmpty(dataFromTempFile2) || string.IsNullOrEmpty(dataFromTempFile3))
			{
				if (this.tvwFamilyList.SelectedNode == null)
				{
					this.SetDefaultItem();
				}
				return;
			}
			if (dataFromTempFile == "当前项目")
			{
				this.rdoCurrentDoc.Checked = true;
			}
			foreach (object obj in this.tvwFamilyList.Nodes[0].Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Text == dataFromTempFile2)
				{
					if (dataFromTempFile != "当前项目")
					{
						this.AddFamilySymbol(treeNode);
					}
					IEnumerator enumerator2 = treeNode.Nodes.GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj2 = enumerator2.Current;
							TreeNode treeNode2 = (TreeNode)obj2;
							if (treeNode2.Text == dataFromTempFile3)
							{
								this.tvwFamilyList.SelectedNode = treeNode2;
								this.tvwFamilyList.Refresh();
								this.ModifyNodeColor();
								break;
							}
						}
						break;
					}
				}
			}
			if (this.tvwFamilyList.SelectedNode == null)
			{
				this.SetDefaultItem();
			}
		}

		private void SetDefaultItem()
		{
			TreeNode treeNode = null;
			try
			{
				TreeNode firstNode = this.tvwFamilyList.Nodes[0].FirstNode;
				this.AddFamilySymbol(firstNode);
				treeNode = firstNode.FirstNode;
			}
			catch
			{
				treeNode = this.tvwFamilyList.Nodes[0];
			}
			if (treeNode != null)
			{
				this.tvwFamilyList.SelectedNode = treeNode;
				this.tvwFamilyList.Refresh();
				this.ModifyNodeColor();
			}
		}

		private void initCbxDoorHeight()
		{
			try
			{
				int num = Convert.ToInt32(Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, "门插入", "高度"));
				this.cbxHeight.Text = num.ToString();
			}
			catch
			{
				this.cbxHeight.Text = "0";
			}
		}

		private void initCbxWinHeight()
		{
			try
			{
				int num = Convert.ToInt32(Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, "窗插入", "高度"));
				this.cbxHeight.Text = num.ToString();
			}
			catch
			{
				this.cbxHeight.Text = "600";
			}
		}

		private void initDoorCbxDis()
		{
			try
			{
				double num = Convert.ToDouble(Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, "门插入", "距离"));
				this.cbxDis.Text = num.ToString();
			}
			catch
			{
				this.cbxDis.Text = "200";
			}
		}

		private void initWinCbxDis()
		{
			try
			{
				double num = Convert.ToDouble(Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, "窗插入", "距离"));
				this.cbxDis.Text = num.ToString();
			}
			catch
			{
				this.cbxDis.Text = "200";
			}
		}

		private void initWinInsertType()
		{
			try
			{
				string dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, "窗插入", "插入类型");
				string a;
				if ((a = dataFromTempFile) != null)
				{
					if (a == "eByPickPoint")
					{
						this.radioBtnPick.Checked = true;
						this.m_nInsertType = InsertType.eByPickPoint;
						goto IL_10E;
					}
					if (a == "eByButtress")
					{
						this.radioBtnButtf.Checked = true;
						this.cbxDis.Visible = true;
						this.labelDis.Visible = true;
						this.m_nInsertType = InsertType.eByButtress;
						goto IL_10E;
					}
					if (a == "eByEqual")
					{
						this.radioBtnEqul.Checked = true;
						this.cbxPrompt.Visible = true;
						this.labelPrompt.Visible = true;
						this.m_nInsertType = InsertType.eByEqual;
						goto IL_10E;
					}
					if (a == "eByGridLineEqual")
					{
						this.cbxPrompt.Visible = true;
						this.labelPrompt.Visible = true;
						this.radioBtnEqualByGrid.Checked = true;
						this.m_nInsertType = InsertType.eByGridLineEqual;
						goto IL_10E;
					}
				}
				this.radioBtnPick.Checked = true;
				this.m_nInsertType = InsertType.eByPickPoint;
				IL_10E:;
			}
			catch
			{
				this.radioBtnPick.Checked = true;
			}
		}

		private void initDoorInsertType()
		{
			try
			{
				string dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, "门插入", "插入类型");
				string a;
				if ((a = dataFromTempFile) != null)
				{
					if (a == "eByPickPoint")
					{
						this.radioBtnPick.Checked = true;
						this.m_nInsertType = InsertType.eByPickPoint;
						goto IL_10E;
					}
					if (a == "eByButtress")
					{
						this.radioBtnButtf.Checked = true;
						this.cbxDis.Visible = true;
						this.labelDis.Visible = true;
						this.m_nInsertType = InsertType.eByButtress;
						goto IL_10E;
					}
					if (a == "eByEqual")
					{
						this.radioBtnEqul.Checked = true;
						this.cbxPrompt.Visible = true;
						this.labelPrompt.Visible = true;
						this.m_nInsertType = InsertType.eByEqual;
						goto IL_10E;
					}
					if (a == "eByGridLineEqual")
					{
						this.cbxPrompt.Visible = true;
						this.labelPrompt.Visible = true;
						this.radioBtnEqualByGrid.Checked = true;
						this.m_nInsertType = InsertType.eByGridLineEqual;
						goto IL_10E;
					}
				}
				this.radioBtnPick.Checked = true;
				this.m_nInsertType = InsertType.eByPickPoint;
				IL_10E:;
			}
			catch
			{
				this.radioBtnPick.Checked = true;
			}
		}

		private void initDoorCbxPrompt()
		{
			try
			{
				int num = Convert.ToInt32(Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, "门插入", "数量"));
				this.cbxPrompt.Text = num.ToString();
			}
			catch
			{
				this.cbxPrompt.Text = "2";
			}
		}

		private void initWinCbxPrompt()
		{
			try
			{
				int num = Convert.ToInt32(Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, "窗插入", "数量"));
				this.cbxPrompt.Text = num.ToString();
			}
			catch
			{
				this.cbxPrompt.Text = "2";
			}
		}

		private void initTagInfo()
		{
			string noodName = "";
			if (this.isDoor)
			{
				noodName = "门插入";
			}
			else
			{
				noodName = "窗插入";
			}
			try
			{
				string dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, noodName, "添加标记");
				if (!string.IsNullOrEmpty(dataFromTempFile))
				{
					bool flag = Convert.ToBoolean(dataFromTempFile);
					if (flag)
					{
						this.chbAddTag.Checked = true;
					}
					else
					{
						this.chbAddTag.Checked = false;
					}
				}
				dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, noodName, "标记样式");
				foreach (object obj in this.cbxSymbol.Items)
				{
					DbFamily dbFamily = obj as DbFamily;
					if (dbFamily.Name == dataFromTempFile)
					{
						this.cbxSymbol.SelectedItem = obj;
						break;
					}
				}
				dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, noodName, "标记文字");
				if (!string.IsNullOrEmpty(dataFromTempFile))
				{
					this.tbxStart.Text = dataFromTempFile;
				}
				dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, noodName, "标记编号");
				if (!string.IsNullOrEmpty(dataFromTempFile))
				{
					this.tbxTag.Text = dataFromTempFile;
				}
				if (this.tbxTag.Text == "<默认编号>")
				{
					this.tbxStart.Enabled = false;
				}
				else
				{
					this.tbxStart.Enabled = true;
				}
			}
			catch
			{
			}
		}

		private void InitType(bool isDoor)
		{
			if (!isDoor)
			{
				this.cbxSymbol.Items.Clear();
				List<DbCategory> all = this.sysDB.CategoryTable.GetAll();
				foreach (DbCategory dbCategory in all)
				{
					if (dbCategory.Name == "窗标记")
					{
						List<DbFamily> allFamilies = dbCategory.GetAllFamilies(false, CheckType.All);
						foreach (DbFamily item in allFamilies)
						{
							this.cbxSymbol.Items.Add(item);
						}
					}
				}
				string dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, "窗插入", "标记样式");
				foreach (object obj in this.cbxSymbol.Items)
				{
					string a = obj.ToString();
					if (a == dataFromTempFile)
					{
						this.cbxSymbol.SelectedItem = obj;
						break;
					}
					if (this.cbxSymbol.Items.Count > 0)
					{
						this.cbxSymbol.SelectedIndex = 0;
					}
				}
				this.tbxStart.Text = "";
				return;
			}
			List<DbCategory> all2 = this.sysDB.CategoryTable.GetAll();
			foreach (DbCategory dbCategory2 in all2)
			{
				if (dbCategory2.Name == "门标记")
				{
					List<DbFamily> allFamilies2 = dbCategory2.GetAllFamilies(false, CheckType.All);
					foreach (DbFamily item2 in allFamilies2)
					{
						this.cbxSymbol.Items.Add(item2);
					}
				}
			}
			string dataFromTempFile2 = Assist.FileUtil.GetDataFromTempFile(this.tempInfo.FullName, "门插入", "标记样式");
			foreach (object obj2 in this.cbxSymbol.Items)
			{
				string a2 = obj2.ToString();
				if (a2 == dataFromTempFile2)
				{
					this.cbxSymbol.SelectedItem = obj2;
					break;
				}
				if (this.cbxSymbol.Items.Count > 0)
				{
					this.cbxSymbol.SelectedIndex = 0;
				}
			}
			this.tbxStart.Text = "";
		}

		private bool IsCheckSuccessful(string param_Name, string param_Value)
		{
			bool result;
			try
			{
				if (string.IsNullOrEmpty(param_Value))
				{
					MessageBox.Show(param_Name + "不能为空。", "盈建科提示");
					result = false;
				}
				else if (Convert.ToDouble(param_Value) < 0.0)
				{
					MessageBox.Show(param_Name + "必须大于0。", "盈建科提示");
					result = false;
				}
				else
				{
					result = true;
				}
			}
			catch (Exception)
			{
				MessageBox.Show(param_Name + "输入不正确。");
				result = false;
			}
			return result;
		}

		private void radioBtnPick_Click(object sender, EventArgs e)
		{
			this.m_nInsertType = InsertType.eByPickPoint;
			this.labelPrompt.Visible = false;
			this.cbxPrompt.Visible = false;
			this.labelDis.Visible = false;
			this.cbxDis.Visible = false;
			this.radioBtnPick.Checked = true;
			this.radioBtnEqualByGrid.Checked = false;
			this.radioBtnButtf.Checked = false;
			this.radioBtnEqul.Checked = false;
		}

		private void radioBtnButtf_Click(object sender, EventArgs e)
		{
			this.m_nInsertType = InsertType.eByButtress;
			this.cbxDis.Visible = true;
			this.labelDis.Visible = true;
			this.cbxPrompt.Visible = false;
			this.labelPrompt.Visible = false;
			this.radioBtnPick.Checked = false;
			this.radioBtnEqul.Checked = false;
			this.radioBtnButtf.Checked = true;
			this.radioBtnEqualByGrid.Checked = false;
		}

		private void radioBtnEqul_Click(object sender, EventArgs e)
		{
			this.m_nInsertType = InsertType.eByEqual;
			this.cbxPrompt.Visible = true;
			this.labelPrompt.Visible = true;
			this.cbxDis.Visible = false;
			this.labelDis.Visible = false;
			this.radioBtnPick.Checked = false;
			this.radioBtnEqul.Checked = true;
			this.radioBtnButtf.Checked = false;
			this.radioBtnEqualByGrid.Checked = false;
		}

		private void radioBtnEqualByGrid_Click(object sender, EventArgs e)
		{
			this.m_nInsertType = InsertType.eByGridLineEqual;
			this.cbxPrompt.Visible = true;
			this.labelPrompt.Visible = true;
			this.cbxDis.Visible = false;
			this.labelDis.Visible = false;
			this.radioBtnEqul.Checked = false;
			this.radioBtnPick.Checked = false;
			this.radioBtnEqualByGrid.Checked = true;
			this.radioBtnButtf.Checked = false;
		}

		private void tbxTag_Leave(object sender, EventArgs e)
		{
			if (this.tbxTag.Text == "")
			{
				this.tbxTag.Text = "<默认编号>";
				this.tbxStart.Enabled = false;
			}
		}

		private void tbxTag_MouseClick(object sender, MouseEventArgs e)
		{
			if (this.tbxTag.Text == "<默认编号>")
			{
				this.tbxTag.Text = "";
				this.tbxStart.Enabled = true;
			}
		}

		private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			TreeNode selectedNode = this.tvwFamilyList.SelectedNode;
			string text = selectedNode.FirstNode.Text;
			if (this.rdoFamilyLib.Checked)
			{
				DbFamily dbFamily = selectedNode.Tag as DbFamily;
				List<string> symbols = dbFamily.Symbols;
				List<ParameterData> list = new List<ParameterData>();
				if (!this.GetParameterData(ref list))
				{
					return;
				}
				if (list.Count == 0)
				{
					MessageBox.Show("无有效尺寸参数（缺少尺寸参数或者参数分组错误）。", "盈建科提示");
					return;
				}
				using (DlgNewSize dlgNewSize = new DlgNewSize(text, symbols, list))
				{
					if (dlgNewSize.ShowDialog() != DialogResult.OK)
					{
						return;
					}
					string newTypeName = dlgNewSize.NewTypeName;
					DataTable dataTable = dbFamily.Database.TypeTable.DataTable(dbFamily);
					dataTable.Clear();
					DataRow dataRow = dataTable.NewRow();
					dataRow["规格名"] = newTypeName;
					foreach (ParameterData parameterData in list)
					{
						dataRow[parameterData.ParName] = AssistFunc.feetToMM(parameterData.ParValue);
					}
					dataTable.Rows.Add(dataRow);
					dbFamily.AddType(dataTable);
					TreeNode treeNode = new TreeNode();
					treeNode.Text = newTypeName;
					treeNode.Tag = dbFamily;
					selectedNode.Nodes.Add(treeNode);
					DlgWinDoorInsert.NewSysmbolSort(selectedNode);
					this.tvwFamilyList.SelectedNode = treeNode;
					return;
				}
			}
			if (this.rdoCurrentDoc.Checked)
			{
				List<string> list2 = new List<string>();
				FamilySymbol familySymbol = selectedNode.FirstNode.Tag as FamilySymbol;
				Family family = selectedNode.Tag as Family;
				List<FamilySymbol> symbols2 = family.GetSymbols();
				List<FamilySymbol> list3 = (from ff in symbols2
				orderby ff.Name
				select ff).ToList<FamilySymbol>();
				foreach (FamilySymbol familySymbol2 in list3)
				{
					list2.Add(familySymbol2.Name);
				}
				List<ParameterData> list4 = new List<ParameterData>
				{
					new ParameterData("宽度", familySymbol.get_Parameter(BuiltInParameter.CASEWORK_WIDTH).AsDouble()),
					new ParameterData("高度", familySymbol.get_Parameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).AsDouble())
				};
				using (DlgNewSize dlgNewSize2 = new DlgNewSize(text, list2, list4))
				{
					if (dlgNewSize2.ShowDialog() == DialogResult.OK)
					{
						string newTypeName2 = dlgNewSize2.NewTypeName;
						Transaction transaction = new Transaction(this.m_document, "Duplicate FamilySymbol");
						transaction.Start();
						FamilySymbol familySymbol3 = familySymbol.Duplicate(newTypeName2) as FamilySymbol;
						familySymbol3.get_Parameter(BuiltInParameter.CASEWORK_WIDTH).Set(list4[0].ParValue);
						familySymbol3.get_Parameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).Set(list4[1].ParValue);
						transaction.Commit();
						TreeNode treeNode2 = new TreeNode();
						treeNode2.Text = newTypeName2;
						treeNode2.Tag = familySymbol3;
						selectedNode.Nodes.Add(treeNode2);
						DlgWinDoorInsert.NewSysmbolSort(selectedNode);
						this.tvwFamilyList.SelectedNode = selectedNode;
					}
				}
			}
		}

		private static void NewSysmbolSort(TreeNode currentNode)
		{
			List<TreeNode> list = new List<TreeNode>();
			foreach (object obj in currentNode.Nodes)
			{
				TreeNode item = (TreeNode)obj;
				list.Add(item);
			}
			List<TreeNode> list2 = (from ff in list
			orderby ff.Text
			select ff).ToList<TreeNode>();
			currentNode.Nodes.Clear();
			foreach (TreeNode node in list2)
			{
				currentNode.Nodes.Add(node);
			}
		}

		public TreeNode FindNodeByText(TreeNodeCollection tns, string strNodeText)
		{
			foreach (object obj in tns)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Text == strNodeText)
				{
					return treeNode;
				}
			}
			return null;
		}

		private bool GetParameterData(ref List<ParameterData> parameterDataList)
		{
			DbFamily dbFamily = this.tvwFamilyList.SelectedNode.Tag as DbFamily;
			if (dbFamily == null)
			{
				return false;
			}
			if (this.archDB == null)
			{
				return false;
			}
			string text = this.tvwFamilyList.SelectedNode.FirstNode.Text;
			DbFamilySymbol dbFamilySymbol = this.archDB.TypeTable[dbFamily, text];
			if (dbFamilySymbol == null)
			{
				return false;
			}
			List<DbParameter> @params = dbFamilySymbol.Params;
			foreach (DbParameter dbParameter in @params)
			{
				double parValue = 0.0;
				if (!this.isDoor)
				{
					if ("默认窗台高度" == dbParameter.ParamDefinition.Name)
					{
						continue;
					}
				}
				try
				{
					parValue = AssistFunc.mmToFeet(Convert.ToDouble(dbParameter.Value.ToString()));
				}
				catch (Exception)
				{
					continue;
				}
				ParameterData item = new ParameterData(dbParameter.ParamDefinition.Name, parValue);
				parameterDataList.Add(item);
			}
			return true;
		}

		private void rdoFamilyLib_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoFamilyLib.Checked)
			{
				this.tvwFamilyList.Nodes.Clear();
				this.tvwFamilyList.Nodes.Add(this.nodesFamilyLib);
				this.tvwFamilyList.Nodes[0].Expand();
				if (this.tvwFamilyList.SelectedNode == null && this.nodeFamilyLib != null)
				{
					this.tvwFamilyList.SelectedNode = this.nodeFamilyLib;
					this.tvwFamilyList.Refresh();
					this.ModifyNodeColor();
				}
			}
		}

		private void rdoCurrentDoc_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoCurrentDoc.Checked)
			{
				this.tvwFamilyList.Nodes.Clear();
				this.tvwFamilyList.Nodes.Add(this.nodesDoc);
				this.tvwFamilyList.Nodes[0].Expand();
				if (this.tvwFamilyList.SelectedNode == null && this.nodeDoc != null)
				{
					this.tvwFamilyList.SelectedNode = this.nodeDoc;
					this.tvwFamilyList.Refresh();
					this.ModifyNodeColor();
				}
			}
		}

		private void tvwFamilyList_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			TreeNode selectedNode = this.tvwFamilyList.SelectedNode;
			if (selectedNode != null)
			{
                selectedNode.BackColor = System.Drawing.Color.Empty;
                selectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void ModifyNodeColor()
		{
			TreeNode selectedNode = this.tvwFamilyList.SelectedNode;
			if (selectedNode != null)
			{
                selectedNode.BackColor = System.Drawing.Color.RoyalBlue;
                selectedNode.ForeColor = System.Drawing.Color.White;
			}
		}

		private void tvwFamilyList_Leave(object sender, EventArgs e)
		{
			this.ModifyNodeColor();
		}

		private void tvwFamilyList_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.GetNodeImage(e);
		}

		private void GetNodeImage(TreeViewEventArgs e)
		{
			TreeNode node = e.Node;
			bool flag;
			if (this.rdoFamilyLib.Checked)
			{
				this.nodeFamilyLib = node;
				flag = true;
			}
			else
			{
				this.nodeDoc = node;
				flag = false;
			}
			if (node.Level == 2)
			{
				if (node.Tag is DbFamily)
				{
					DbFamily dbFamily = (DbFamily)node.Tag;
					string fullName = this.archDB.FamilyFileTable[dbFamily].FullName;
					Document doc = this.Get3DPreviewPicture(fullName);
					this.Update3DView(doc);
					this.Refresh2DImg(doc, dbFamily);
					return;
				}
				if (node.Tag is FamilySymbol)
				{
					FamilySymbol familySymbol = (FamilySymbol)node.Tag;
					Document doc2 = this.m_document.EditFamily(familySymbol.Family);
					this.Update3DView(doc2);
					this.Refresh2DImg(doc2, familySymbol);
					return;
				}
			}
			else if (node.Level == 1)
			{
				if (flag)
				{
					this.AddFamilySymbol(node);
				}
				if (node.FirstNode.Tag is DbFamily)
				{
					DbFamily dbFamily2 = (DbFamily)node.FirstNode.Tag;
					string fullName2 = this.archDB.FamilyFileTable[dbFamily2].FullName;
					Document doc3 = this.Get3DPreviewPicture(fullName2);
					this.Update3DView(doc3);
					this.Refresh2DImg(doc3, dbFamily2);
					return;
				}
				if (node.FirstNode.Tag is FamilySymbol)
				{
					FamilySymbol familySymbol2 = (FamilySymbol)node.FirstNode.Tag;
					Document doc4 = this.m_document.EditFamily(familySymbol2.Family);
					this.Update3DView(doc4);
					this.Refresh2DImg(doc4, familySymbol2);
				}
			}
		}

		private void Refresh2DImg(Document doc, DbFamily dbFamily)
		{
			this.legend = LegendCommon.GetFamily2DImg(this.m_application, doc, LegendType.DoorWindows);
			if (this.legend != null)
			{
				this.pictureBox1.Enabled = true;
				this.pbPreview.Enabled = false;
				this.InfoChanged = true;
				this.pictureBox1.BringToFront();
				this.pictureBox1.Refresh();
				return;
			}
			this.pictureBox1.Enabled = false;
			this.pbPreview.Enabled = true;
			this.pbPreview.BringToFront();
			this.pbPreview.Image = AssistFunc.ResizeImage((Bitmap)dbFamily.Image, this.imgW, this.imgH);
		}

		private void Refresh2DImg(Document doc, FamilySymbol fs)
		{
			this.legend = LegendCommon.GetFamily2DImg(this.m_application, doc, LegendType.DoorWindows);
			if (this.legend != null)
			{
				this.pictureBox1.Enabled = true;
				this.pbPreview.Enabled = false;
				this.InfoChanged = true;
				this.pictureBox1.BringToFront();
				this.pictureBox1.Refresh();
				return;
			}
			this.pictureBox1.Enabled = false;
			this.pbPreview.Enabled = true;
			this.pbPreview.BringToFront();
			this.pbPreview.Image = fs.GetPreviewImage(new Size(this.imgW, this.imgH));
		}

		private void AddFamilySymbol(TreeNode node)
		{
			if (node.Nodes.Count == 1 && node.Nodes[0].Text == "Temp")
			{
				node.Nodes.Clear();
				DbFamily dbFamily = node.Tag as DbFamily;
				List<string> symbols = dbFamily.Symbols;
				symbols.Sort();
				if (symbols.Count > 0)
				{
					for (int i = 0; i < symbols.Count; i++)
					{
						TreeNode treeNode = new TreeNode();
						treeNode.Text = symbols[i];
						treeNode.Tag = dbFamily;
						node.Nodes.Add(treeNode);
					}
					this.tvwFamilyList.Refresh();
				}
			}
		}

		private void tvwFamilyList_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
                System.Drawing.Point pt = new System.Drawing.Point(e.X, e.Y);
				TreeNode nodeAt = this.tvwFamilyList.GetNodeAt(pt);
				if (nodeAt != null)
				{
					if (nodeAt.Level == 1)
					{
						nodeAt.ContextMenuStrip = this.cmsNewMenu;
						this.tvwFamilyList.SelectedNode = nodeAt;
					}
					if (nodeAt.Level == 0)
					{
						if (!this.btnNext.Visible)
						{
							nodeAt.ContextMenuStrip = this.cmsSearch;
						}
						this.tvwFamilyList.SelectedNode = nodeAt;
					}
				}
			}
		}

		private void toolStripMenuItem1_Click(object sender, EventArgs e)
		{
			DlgSearch dlgSearch = new DlgSearch();
			DialogResult dialogResult = dlgSearch.ShowDialog();
			if (dialogResult == DialogResult.OK)
			{
				string searchContent = dlgSearch.SearchContent;
				if (string.IsNullOrEmpty(searchContent))
				{
					MessageBox.Show("搜索内容不能为空。", "盈建科提示");
					return;
				}
				List<TreeNode> list = this.FindNode(this.tvwFamilyList.Nodes[0], searchContent);
				if (list.Count > 0)
				{
					TreeNode treeNode = new TreeNode();
					if (this.isDoor)
					{
						treeNode.Text = "门";
					}
					else
					{
						treeNode.Text = "窗";
					}
					foreach (TreeNode treeNode2 in list)
					{
						treeNode.Nodes.Add((TreeNode)treeNode2.Clone());
					}
					this.tvwFamilyList.Nodes.Clear();
					this.tvwFamilyList.Nodes.Add(treeNode);
					this.tvwFamilyList.Nodes[0].Expand();
					this.SetSearchButton(true);
					this.tvwFamilyList.Size = new Size(168, 259);
					this.SetSearchDefaultItem();
					return;
				}
				MessageBox.Show("没有检索到符合条件的族。", "盈建科提示");
			}
		}

		private List<TreeNode> FindNode(TreeNode tnParent, string strValue)
		{
			List<TreeNode> list = new List<TreeNode>();
			foreach (object obj in tnParent.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Text.Contains(strValue))
				{
					list.Add(treeNode);
				}
			}
			return list;
		}

		private void SetSearchButton(bool bo)
		{
			this.btnNext.Visible = bo;
			this.btnPrevious.Visible = bo;
			this.btnClose.Visible = bo;
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			this.SetSearchButton(false);
			this.tvwFamilyList.Nodes.Clear();
			if (this.rdoCurrentDoc.Checked)
			{
				this.tvwFamilyList.Nodes.Add(this.nodesDoc);
			}
			else
			{
				this.tvwFamilyList.Nodes.Add(this.nodesFamilyLib);
			}
			this.tvwFamilyList.Nodes[0].Expand();
			this.tvwFamilyList.Size = new Size(168, 288);
		}

		private void btnNext_Click(object sender, EventArgs e)
		{
			this.SelectNode(this.tvwFamilyList.Nodes[0].LastNode, "已经到达最后一个。", true);
		}

		private void btnPrevious_Click(object sender, EventArgs e)
		{
			this.SelectNode(this.tvwFamilyList.Nodes[0].FirstNode, "已经到达第一个。", false);
		}

		private void SelectNode(TreeNode node, string p, bool isNext)
		{
			TreeNode treeNode = this.tvwFamilyList.SelectedNode;
			if (treeNode.Level == 0)
			{
				treeNode = this.tvwFamilyList.Nodes[0].FirstNode;
			}
			else if (treeNode.Level == 2)
			{
				treeNode = treeNode.Parent;
			}
			if (treeNode == node)
			{
				MessageBox.Show(p, "盈建科科技");
				return;
			}
			TreeNode treeNode2;
			if (isNext)
			{
				treeNode2 = treeNode.NextNode;
			}
			else
			{
				treeNode2 = treeNode.PrevNode;
			}
			if (treeNode2 != null)
			{
				this.tvwFamilyList.SelectedNode = treeNode2;
				this.tvwFamilyList.Refresh();
				this.ModifyNodeColor();
			}
		}

		private void SetSearchDefaultItem()
		{
			TreeNode treeNode = null;
			try
			{
				treeNode = this.tvwFamilyList.Nodes[0].FirstNode;
			}
			catch
			{
				treeNode = this.tvwFamilyList.Nodes[0];
			}
			if (treeNode != null)
			{
				this.tvwFamilyList.SelectedNode = treeNode;
				this.tvwFamilyList.Refresh();
				this.ModifyNodeColor();
			}
		}

		private void DlgWinDoorInsert_FormClosed(object sender, FormClosedEventArgs e)
		{
		}

		private void tvwFamilyList_AfterExpand(object sender, TreeViewEventArgs e)
		{
			TreeNode node = e.Node;
			bool @checked = this.rdoFamilyLib.Checked;
			if (@checked && node.Level == 1)
			{
				this.AddFamilySymbol(node);
			}
		}

		private Document Get3DPreviewPicture(string path)
		{
			Document document = null;
			try
			{
				document = this.m_application.Application.OpenDocumentFile(path);
				this.m_GeneralDocumentList.Add(document);
			}
			catch (Exception)
			{
				MessageBox.Show("文档打开出错", "盈建科提示");
			}
			return document;
		}

		private void Update3DView(Document doc)
		{
			PreviewControl previewControl = this.elementHost.Child as PreviewControl;
			if (doc == null)
			{
				if (previewControl != null)
				{
					previewControl.Dispose();
					previewControl = null;
				}
				return;
			}
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
            filteredElementCollector.OfClass(typeof(Autodesk.Revit.DB.View));
            IEnumerable<Autodesk.Revit.DB.View> enumerable = from Element f in filteredElementCollector
                                                             where (f as Autodesk.Revit.DB.View).CanBePrinted
                                                             select f as Autodesk.Revit.DB.View;
            Autodesk.Revit.DB.View view = null;
            foreach (Autodesk.Revit.DB.View view2 in enumerable)
			{
                if ((int)view2.ViewType == 4 && view2.Name == "{3D}")
				{
					view = view2;
					break;
				}
			}
			if (view == null)
			{
                foreach (Autodesk.Revit.DB.View view3 in enumerable)
				{
                    if ((int)view3.ViewType == 4)
					{
						view = view3;
						break;
					}
				}
			}
			if (view == null)
			{
                foreach (Autodesk.Revit.DB.View view4 in enumerable)
				{
                    if ((int)view4.ViewType == 1)
					{
						view = view4;
						break;
					}
				}
			}
			if (view == null)
			{
				if (previewControl != null)
				{
					previewControl.Dispose();
					previewControl = null;
				}
				return;
			}
			if (previewControl != null)
			{
				previewControl.Dispose();
			}
            this.elementHost.Child = new PreviewControl(doc, view.Id);
		}

		public void CloseDocuments()
		{
			foreach (Document document in this.m_GeneralDocumentList)
			{
				try
				{
					document.Close(false);
				}
				catch (Exception)
				{
				}
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.DialogResult = DialogResult.Cancel;
			}
			return false;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.tvwFamilyList.SelectedNode == null || this.tvwFamilyList.SelectedNode.Level != 2)
			{
				MessageBox.Show("请选择族类型。", "盈建科提示");
				return;
			}
			if (!this.IsCheckSuccessful(this.isDoor ? "门槛高" : "窗台高", this.cbxHeight.Text))
			{
				return;
			}
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			this.m_datum.m_nInsertType = this.m_nInsertType;
			if (this.m_nInsertType == InsertType.eByEqual)
			{
				if (!this.IsCheckSuccessful("个数", this.cbxPrompt.Text))
				{
					return;
				}
				this.m_datum.m_nEqualNum = Convert.ToInt32(this.cbxPrompt.Text);
			}
			else if (this.m_nInsertType == InsertType.eByButtress)
			{
				if (!this.IsCheckSuccessful("垛宽", this.cbxDis.Text))
				{
					return;
				}
				this.m_datum.m_dButtressWidth = Convert.ToDouble(this.cbxDis.Text);
			}
			else if (this.m_nInsertType == InsertType.eByGridLineEqual)
			{
				if (!this.IsCheckSuccessful("个数", this.cbxPrompt.Text))
				{
					return;
				}
				this.m_datum.m_nEqualNum = Convert.ToInt32(this.cbxPrompt.Text);
			}
			string name;
			string value;
			if (this.rdoFamilyLib.Checked)
			{
				DbFamily dbFamily = (DbFamily)this.tvwFamilyList.SelectedNode.Parent.Tag;
				string text = this.tvwFamilyList.SelectedNode.Text;
				this.m_datum.m_symbol = dbFamily.Database.TypeTable[dbFamily, text];
				name = dbFamily.Name;
				value = text;
				dictionary.Add("族来源", "族库");
			}
			else
			{
				this.familySymbolDoc = (this.tvwFamilyList.SelectedNode.Tag as FamilySymbol);
				name = (this.tvwFamilyList.SelectedNode.Parent.Tag as Family).Name;
				value = this.familySymbolDoc.Name;
				dictionary.Add("族来源", "当前项目");
			}
			dictionary.Add("族名", name);
			dictionary.Add("类型名", value);
			dictionary.Add("插入类型", this.m_nInsertType.ToString());
			dictionary.Add("高度", this.cbxHeight.Text);
			dictionary.Add("数量", this.cbxPrompt.Text);
			dictionary.Add("距离", this.cbxDis.Text);
			dictionary.Add("添加标记", this.chbAddTag.Checked.ToString());
			dictionary.Add("标记样式", this.cbxSymbol.Text);
			dictionary.Add("标记文字", this.tbxStart.Text);
			dictionary.Add("标记编号", this.tbxTag.Text);
			if (this.isDoor)
			{
				Assist.FileUtil.WriteTempData(this.tempInfo.FullName, "门插入", ConfigData.DoorWinData, dictionary);
			}
			else
			{
				Assist.FileUtil.WriteTempData(this.tempInfo.FullName, "窗插入", ConfigData.DoorWinData, dictionary);
			}
			base.DialogResult = DialogResult.OK;
		}

		public InsertType m_nInsertType = InsertType.eByPickPoint;

		public DoorWinDatum m_datum;

		public FamilySymbol familySymbolDoc;

		public static BuiltInCategory bc = BuiltInCategory.OST_Doors;

		private FileInfo tempInfo;

		private UIApplication m_application;

		private Document m_document;

		private FamilyLibDatabase archDB;

		private FamilyLibDatabase sysDB;

		private bool isDoor;

		private TreeNode nodesDoc;

		private TreeNode nodesFamilyLib;

		private TreeNode nodeDoc;

		private TreeNode nodeFamilyLib;

		private List<Document> m_GeneralDocumentList = new List<Document>();

		private int imgW;

		private int imgH;

		private Previewer pictureBox1;

		private bool InfoChanged;

		private Legend legend;
	}
}
